//
//  CHJVideoEditManger.h
//  CHJVideoKit_Example
//
//  Created by jiaxuzhou on 2017/11/30.
//  Copyright © 2017年 jxzjxz. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "CHJCaptureDataSource.h"
#import <AVFoundation/AVFoundation.h>
#import <Photos/Photos.h>
#import "YXVideoEffectModel.h"
#import "YXVideoPasterObject.h"
#import "YXVideoTextModel.h"
#import "YXVideoSDKDriver.h"

@class CHJVideoEditManger;

@protocol CHJVideoEditMangerDelegate <NSObject>


/**
 播放回调
 
 @param driver driver实例
 @param time 播放当前时间
 @param duration 总时间
 */
- (void)chj_driver:(CHJVideoEditManger *)driver played:(CGFloat)time total:(CGFloat)duration;



/**
 开始播放回调
 
 @param driver driver实例
 */
- (void)chj_driverDoPlay:(CHJVideoEditManger *)driver;


/**
 暂停播放回调
 
 @param driver driver实例
 */
- (void)chj_driverDoPause:(CHJVideoEditManger *)driver;


/**
 完成整个视频播放回调
 
 @param driver driver实例
 */
- (void)chj_driverPlayback:(CHJVideoEditManger *)driver;


/**
 加载视频播放失败回调, 需要重新加载
 
 @param driver driver实例
 */
- (void)chj_driverLoadVideoFailed:(CHJVideoEditManger *)driver;


/**
 变声服务开始
 
 @param driver driver
 */
- (void)chj_driverVoiceChangeServiceStarted:(CHJVideoEditManger *)driver;

/**
 变声服务完成
 
 @param driver driver
 */
- (void)chj_driverVoiceChangeServiceDone:(CHJVideoEditManger *)driver;

/**
 变声服务取消
 
 @param driver driver
 */
- (void)chj_driverVoiceChangeServiceCancelled:(CHJVideoEditManger *)driver;

@end


@interface CHJVideoEditManger : NSObject

@property (nonatomic, weak) id<CHJVideoEditMangerDelegate> delegate;

@property (nonatomic, strong) YXVideoSDKDriver *videoEngine;

@property (nonatomic, strong) UIImage *orgimage;

/**
 从拍摄的视频片段进入

 @return CHJVideoEditManger
 */
- (id)initWithCapture;


/**
 从手机相册里选择的视频
 @param assetP 手机相册元素对象
 @return CHJVideoEditManger
 */
- (void)initWithPHAsset:(PHAsset *)assetP progress:(void (^)(CGFloat flt))progressblock finish:(void (^)(BOOL isSuccess))finish;

- (void)initWithPHAsset:(PHAsset *)assetP composition:(AVComposition *)composition videoComposition:(AVVideoComposition *)videoComposition audioMix:(AVAudioMix *)audioMix progress:(void (^)(CGFloat flt))progressblock finish:(void (^)(BOOL isSuccess))finish;

- (void)initWithAVAsset:(AVAsset *)asset;

- (void)initWithAsset:(AVAsset *)asset frame:(CGRect)rect;

+ (void)getVideoParamByRect:(CGRect)rect asset:(AVAsset *)asset timeRange:(CMTimeRange)timeRange composition:(AVMutableComposition **)composition videoComposition:(AVMutableVideoComposition **)videoComposition outAudioMix:(AVAudioMix **)outAudioMix;

+ (void)exportVideoParamByRect:(CGRect)rect asset:(AVAsset *)asset timeRange:(CMTimeRange)timeRange outputPath:(NSString *)outputPath transcoding:(NSString *)transcoding success:(void (^)(void))success fail:(void (^)(void))fail blockSDK:(void (^)(YXVideoSDKDriver *))blockSDK;


/// 修改播放器背景色 默认hei se
- (void)changeRenderViewClearColor:(UIColor *)color;
- (void)seekWithTextureCallback:(void (^)(void))success;

#pragma mark -- *****************************************  字幕相关  *****************************************

// 字幕
- (void)updateSubtitles:(NSArray <NSDictionary <NSString *, NSString *> *> *)subtitles;

/**
 添加一个字幕 Model
 
 @param textModel model
 */
- (void)arrangeTextModel:(YXVideoTextModel *)textModel;


/**
 移除一个字幕 Model
 
 @param textModel model
 */
- (void)removeTextModel:(YXVideoTextModel *)textModel;


/**
 移除 index位置上的 model
 
 @param index index
 */
- (void)removeTextModelAtIndex:(NSUInteger)index;


/**
 移除所有 model
 */
- (void)removeAllTextModel;


/**
 显示已经加载的 model
 */
- (void)showArrangedTextModel;


/**
 隐藏所有 model
 */
- (void)hideArrangedTextModel;


#pragma mark -- *****************************************  贴纸相关  *****************************************

// 贴纸

/**
 添加一个贴纸
 
 @param paster 贴纸实例
 */
- (void)addPaster:(YXVideoPasterObject *)paster;


/**
 移除一个贴纸
 
 @param paster 贴纸实例
 */
- (void)removePaster:(YXVideoPasterObject *)paster;


/**
 获取全部已经添加的贴纸
 
 @return 贴纸数组
 */
- (NSArray <YXVideoPasterObject *> *)getAddedPasters;


/**
 更新贴纸层
 */
- (void)updatePasterLayer;


/**
 隐藏贴纸层
 */
- (void)hidePasterLayer;


/**
 显示贴纸层
 */
- (void)showPasterLayer;

// layer
- (void)arrangeTextLayerWithText:(NSString *)text
                       textColor:(UIColor *)textColor
                 backgroundColor:(UIColor *)backgroundColor
                            font:(UIFont *)font
                        position:(CGPoint)position
                       transform:(CGAffineTransform)transform
                          forKey:(NSString *)key;

- (void)arrangeLayer:(CALayer *)layer forKey:(NSString *)key;
- (void)removeLayerWithKey:(NSString *)key;

#pragma mark -- *****************************************  UI播放相关  *****************************************

/**
 播放
 */
- (void)play;

/**
 暂停
 */
- (void)pause;

#pragma mark -- *****************************************  声音相关  *****************************************

/**
 变声控制
 
 @param pitch 音调
 @param speed 语速
 */
- (void)suitVideoWithPitch:(NSInteger)pitch speed:(CGFloat)speed;

/**
 视频原声开关
 
 @param enable 是否开启
 */
- (void)enableOriginalSound:(BOOL)enable;

// 音乐
- (void)setMusicVolume:(CGFloat)volume; //背景
- (void)setOriginVolume:(CGFloat)volume; //原音


#pragma mark -- *************************************  切换滤镜和音乐相关  *****************************************

/**
 加载效果 model。在切换滤镜的时候调用
 
 @param videoEffectModel 效果 model
 @param musicPath 如果给视频陪背景音乐 音乐的路径
 @param timeRange 音乐循环区间
 @param doneHandler 完成加载 model回调
 */
- (void)loadVideoEffectModel:(YXVideoEffectModel *)videoEffectModel musicPath:(NSString *)musicPath trackRange:(CMTimeRange)timeRange whenDone:(void (^)())doneHandler;

/**
加载效果 model 切换滤镜没有音乐选择时调用
 @param videoEffectModel 效果 model
 @param doneHandler 完成加载 model回调
 */
- (void)loadVideoEffectModel:(YXVideoEffectModel *)videoEffectModel whenDone:(void (^)())doneHandler;

#pragma mark -- *****************************************  剪辑相关  *****************************************

/**
 seek 使用场景：改变了滑块范围 即：快进 快退

 @param second 时间
 */
- (void)seekToSecondWithHandleDecode:(CGFloat)second;

/**
 seek 使用场景：改变了滑块范围 即：快进 快退

 @param progress 进度百分比
 */
- (void)seekToProgressWithHandleDecode:(CGFloat)progress;

/**
 结束滑动时调用 用于记录停止位置
 */
- (void)seekServiceDone;


#pragma mark -- *****************************************  导出相关  *****************************************

/**
 导出方法
 
 @param path 导出路径
 @param fileType 文件 type 一般 mp4使用 AVFileTypeMPEG4
 @param bitRate 导出文件的比特率
 @param exportingBlock 正在导出的回调
 @param exportedBlock 导出完成的回调
 @param exportCanceledBlock 导出取消的回调
 @param exportFailed 导出失败的回调
 */
- (void)exportVideoToPath:(NSString *)path fileType:(NSString *)fileType bitRate:(NSInteger)bitRate exporting:(void (^)(CGFloat progress))exportingBlock exported:(void (^)())exportedBlock exportCanceled:(void (^)())exportCanceledBlock exportFailed:(void (^)(NSError *error))exportFailed;

/**
 取消导出
 */
- (void)cancelExport;

#pragma mark -- *****************************************  获取资源相关  *****************************************

/// 获取截图
- (void)getThumbnailsWithCount:(NSInteger)count scaled:(CGFloat)scale compeletionC:(void (^)(UIImage *image, NSUInteger index))callback;

/**
 绘制视频的UIView

 @return UIView
 */
- (UIView *)getRenderView;

/**
 视频剪切后的范围

 @return 范围
 */
- (CMTimeRange)getVideoRange;

/**
 获取视频尺寸

 @return 尺寸
 */
- (CGSize)getVideoSize;

/**
 获取视频时长

 @return 时长
 */
- (CGFloat)getVideoDuration;


/**
 是否正在播放视频

 @return YES  NO
 */
- (BOOL)getIsPlaying;

@end
